﻿namespace Photo.Core.Utils
{
    /// <summary>
    /// 随机数帮助类
    /// </summary>
    public class RndUtil
    {
        /// <summary>
        /// 生成0-9随机数
        /// </summary>
        /// <param name="codeNum">生成长度</param>
        /// <returns></returns>
        public static string RndNum(int codeNum)
        {
            StringBuilder sb = new StringBuilder(codeNum);
            Random rand = new Random();
            for (int i = 1; i < codeNum + 1; i++)
            {
                int t = rand.Next(9);
                sb.AppendFormat("{0}", t);
            }
            return sb.ToString();
        }

        //字符集，去掉了一些容易混淆的字符 
        private static char[] character = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
        /// <summary>
        /// 生成随机N位数字或字母
        /// </summary>
        /// <param name="codeNum"></param>
        /// <returns></returns>
        public static string RndStr(int codeNum)
        {
            //生成随机字符
            string inviteCode = string.Empty;

            Random rnd = new Random();
            //生成字符串
            for (int i = 0; i < codeNum; i++)
            {
                inviteCode += character[rnd.Next(character.Length)];
            }

            return inviteCode;
        }
    }
}
